/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.crud.Impl;

import com.startup.musicstore.model.Album;
import com.startup.musicstore.model.AlbumCategory;
import com.startup.musicstore.model.SampleClip;
import com.startup.musicstore.model.Track;
import com.startup.musicstore.services.crud.AlbumCategoryCrudService;
import com.startup.musicstore.services.crud.WebAddressService;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author given
 */
public class WebAddressServiceImpl implements WebAddressService{
    private static ApplicationContext ctx;
    

    @Override
    public String getWebAddress(Long albumCategoryID, Long albumID, Long trackID) {
        ctx = new ClassPathXmlApplicationContext("classpath:com/startup/musicstore/app/conf/applicationContext-*.xml");    
        AlbumCategoryCrudService albumCategoryCrudService = (AlbumCategoryCrudService)ctx.getBean("albumCategoryCrudService");
        AlbumCategory albumCategory = albumCategoryCrudService.findById(albumCategoryID);
        List<Album> albums = albumCategory.getAlbums();
        for(Album a: albums){
            if(albumID.equals(a.getId())){
                List<Track> tracks = a.getTracks();
                for(Track t: tracks){
                    if(trackID.equals(t.getId())){
                        SampleClip sampleClip = t.getSampleClip();
                        return sampleClip.getSampleUrl();
                    }
                }
            }
        }
        return "";
    }
    
}
